<?php

namespace libs\database\mysqli;

class query extends \libs\database{
    
    public static $_connection;
    
    protected $_connector;
    
 
    public function __construct(\mysqli $db) 
    {
        self::$_connection = $db;
    }
    
    public function getAll($tabela)
    {
        $query = "SELECT * FROM {$tabela}"; 
        return $this->executeQuery($query);
    }
    
    public function get_where($tabela, $where = array(),$order = null, $limit = null)
    {
        $template = 'SELECT * FROM %s WHERE %s';
        
        $_where = array();
        
        foreach($where as $column=>$value)
        {
            $_where[] = "{$column}=".$this->quote($value);
        }
        
        if($order != null)
        {
            $_order = array();
            
            foreach($order as $column => $direction)
            {
                $_order[] = " {$column} {$direction}";
            }
            
            $order = implode(',',$_order);
        }
        
        $_where = implode(' AND ',$_where);
        
        $_where .= $order;
        
        $query = sprintf($template,$tabela,$_where);
        
        return $this->executeQuery($query);
    }
   
    public function quote($item)
    {
        $item = self::$_connection->real_escape_string($item);
        
        return "\"$item\"";
    }
    
    public function update($tabela,$pola = array(),$where = array())
    {
        $template = "UPDATE %s SET %s WHERE %s";
        
        $_pola = array();
        
        foreach($pola as $pole=>$wartosc)
        {
            $_pola[] = "{$pole}=".$this->quote($wartosc);
        }
        
        $_pola = implode(', ',$_pola);
        
        $_where = array();
        
        if(is_array($where))
        {
            foreach($where as $key=>$val)
            {
                $_where[] = "{$key}=".$this->quote($val);
            }
            
            $_where = implode(' AND ', $_where);
        }
        else
        {
            $_where = array_values(array_filter(preg_split('/[AND]/',$where,0,PREG_SPLIT_DELIM_CAPTURE), 
                    function($item){ return !empty($item);}));
            $_where = array_map('trim',$_where);
            
            $_where = implode(' AND ',$_where);
        }
        
        
        $query = sprintf($template,$tabela,$_pola,$_where);
        
        return $this->executeQuery($query);
    }
    
    public function insert($tabela,$dane = array())
    {
        $pola = implode(',',array_keys($dane));
        
        $wartosci = implode(',',array_map(array($this,
            'quote'),$dane));
        
        $query = 'INSERT INTO '.$tabela.' (id,'.$pola.') VALUES ("",'.$wartosci.')';
        
        $this->executeQuery($query);
    }
    
    
    public function executeQuery($query)
    {
        $result = self::$_connection->query($query);
        
        if($result->num_rows == 0)
        {
            return null;
        }
        
        while ($row = $result->fetch_assoc())
        {
            $array[] = array_map('stripslashes',$row);
        }
        
        if(count($array) == 1)
        {
            return array_map('stripslashes',$array[0]);
        }

        return $array;  
    }
    
    public function delete($tabela,$pola = array())
    {
        $template = "DELETE FROM %s WHERE %s";
        
        $_pola = array();
        
        foreach($pola as $pole=>$wartosc)
        {
            $_pola[] = "{$pole}=".$this->quote($wartosc);   
        }
        
        $_pola = implode(' AND ',$_pola);
        
        $query = sprintf($template,$tabela,$_pola);
        
        return $this->executeQuery($query);
    }
    
    public function insert_id()
    {
        return self::$_connection->insert_id;
    }
}

?>
